 package com.zqp.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.catalina.authenticator.SpnegoAuthenticator.AuthenticateAction;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import com.fasterxml.jackson.annotation.JsonView;
import com.zqp.dto.User;
import com.zqp.dto.User.UserDetialView;
import com.zqp.dto.User.UserSimpleView;
import com.zqp.exception.UserNotExistException;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/user")
public class UserController {

	
	@Autowired
	private ProviderSignInUtils providerSignInUtils;
	
	
	@PostMapping("/regist")
	public void regist(User user,HttpServletRequest request) {
	//注册用户 ->不管是注册和绑定 都有一个唯一的ID
		String userId = user.getUsername();
		
		providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
	}
	@GetMapping("/me")
	public Object getCurrentUser(@AuthenticationPrincipal UserDetails user) {
//		Authentication auth
//		return SecurityContextHolder.getContext().getAuthentication();
		return user;
	}
	
	@GetMapping
	@JsonView(UserSimpleView.class)
	@ApiOperation(value="用户查询服务")
	public List<User> query(User user) {
//		System.out.println(nickname);
		List<User> list = new ArrayList<User>();
		list.add(new User());
		list.add(new User());
		list.add(new User());
		return list;
	}

	@JsonView(UserDetialView.class)
	@GetMapping("/{id:\\d+}")
	public User getInfo(@ApiParam(value="用户id")@PathVariable String id) {
//		throw new UserNotExistException(id);
		System.out.println("进入  getinfo  服务");
		User user = new User();
		user.setUsername("tom");
		return user;
	}

	@PostMapping
	public User create(@Valid @RequestBody User user) {

//		if (errors.hasErrors()) {
//			errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
//		}

		System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE));
		user.setId("1");
		return user;

	}

	@PutMapping("/{id:\\d+}")
	public User update(@Valid @RequestBody User user, BindingResult errors) {

		if (errors.hasErrors()) {
			errors.getAllErrors().stream().forEach(error -> {
				FieldError fieldError = (FieldError) error;
//				System.out.println(fieldError);
//				System.out.println(fieldError.getField() +" "+fieldError.getDefaultMessage());
				System.out.println(error.getDefaultMessage());
				
			});
		}

		System.out.println(ReflectionToStringBuilder.toString(user, ToStringStyle.MULTI_LINE_STYLE));
		user.setId("1");
		return user;

	}
	@DeleteMapping("/{id:\\d+}")
	public void detele(@PathVariable String id) {
		
		System.out.println("删除的Id"+id);
		
		
	}
}
